package cc.rengu.igas.mcps.common.util;

import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import io.netty.util.internal.StringUtil;

import java.net.URLDecoder;

/**
 * C1二维码工具类
 *
 * @author WANG SHI JUN
 * @version 1.0.0 创建时间：2020/08/13
 */
public class QrCodeUtil {

    protected RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /* 银联订单类型 */
    private static String userOrderTypes = "10,11,12,14,15,16,20,31,40";

    /**
     * 过滤C1码原串中的tt和si信息
     *
     * @param qrUrl
     * @return
     */
    public static String filterTtAndSi(String qrUrl) throws Exception {
        if (qrUrl.indexOf("&si=") > 0) {
            StringBuffer sb = new StringBuffer();
            String paramterData = "";
            if (qrUrl.indexOf("?") > 0) {
                paramterData = qrUrl.substring(qrUrl.indexOf("?") + 1);
                sb.append(qrUrl.substring(0, qrUrl.indexOf("?") + 1));
            } else {
                paramterData = qrUrl.substring(qrUrl.lastIndexOf("/") + 1);
                sb.append(qrUrl.substring(0, qrUrl.lastIndexOf("/") + 1));
            }
            paramterData = URLDecoder.decode(paramterData, "UTF-8");
            //C1码
            String[] tempArray = paramterData.split("\\&");
            for (String item : tempArray) {
                if (item.indexOf("tt=") == -1
                        && item.indexOf("si=") == -1) {
                    sb.append(item);
                    sb.append("&");
                }
            }
            return sb.substring(0, sb.length() - 1).toString();
        }
        return qrUrl;
    }

    /**
     * 获得url中的参数
     *
     * @param url
     * @param paramName
     * @return
     * @throws Exception
     */
    public static String getParamValue(String url, String paramName) throws Exception {
        String paramterData = "";
        if (url.indexOf("?") > 0) {
            paramterData = url.substring(url.indexOf("?") + 1);
        } else {
            paramterData = url.substring(url.lastIndexOf("/") + 1);
        }
        paramterData = URLDecoder.decode(paramterData, "UTF-8");
        String[] tempArray = paramterData.split("&");
        for (String item : tempArray) {
            int index = item.indexOf("=");
            index = index > 0 ? index : 0;
            String key = item.substring(0, index).trim();
            if (key.equals(paramName)) {
                return item.substring(index + 1).trim();
            }
        }
        return "";
    }

    /**
     * c1校验时间戳公共方法
     * 有效期固定为30分钟
     * 有效期内返回true
     * 失效返回false
     *
     * @return
     * @throws Exception
     */
    public static boolean checkTimestampForC1QrCode(String url) throws Exception {
        String timestamp = getParamValue(url, "tt");
        if (StringUtil.isNullOrEmpty(timestamp)) {
            return true;
        }
        Long qrTimes = DateUtil.getTimeStamp(timestamp, "yyyyMMddHHmmss");
        long ct = (System.currentTimeMillis() - qrTimes) / 1000L;
        if (ct > 1800) {
            return false;
        }
        return true;
    }

    /**
     * 检查订单类型是否是银联二维码类型订单
     *
     * @param orderType
     * @return
     */
    public static boolean checkQrOrderType(String orderType) {
        if (null == orderType || "".equals(orderType.trim())) {
            return false;
        }
        if (userOrderTypes.indexOf(orderType) == -1) {
            return true;
        }
        return false;
    }

}
